package com.gexiaobao.pigeon.ui.adapter

import android.graphics.Typeface
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.databinding.ItemFive1StyleBinding

/**
 * @Author      : hxw
 * @Date        : 2024/9/21 9:51
 * @Describe    :
 */
class AdapterFive1Style(data: ArrayList<EditBloodInfoBean>) :
    BaseQuickAdapter<EditBloodInfoBean, DataBindBaseViewHolder>(R.layout.item_five1_style, data) {
    override fun convert(holder: DataBindBaseViewHolder, item: EditBloodInfoBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemFive1StyleBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val tvItemRingId = holder.getView<AppCompatTextView>(R.id.tvItemRingId)

        val ringIdBg: Int
        val sexIcon: Int
        val ringIdTvColor: Int
        when (holder.layoutPosition) {
            0 -> {
                tvItemRingId.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                ringIdBg = R.drawable.round8_male_pigeon_linear_bg
                sexIcon = R.mipmap.icon_male
                ringIdTvColor = R.color.comm_text_gray_dark
            }

            1 -> {
                tvItemRingId.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                ringIdTvColor = R.color.color_f65a5a
                ringIdBg = 0
                sexIcon = when (item.sex) {
                    "雌" -> {
                        R.mipmap.icon_female
                    }

                    "雄" -> {
                        R.mipmap.icon_male
                    }

                    else -> {
                        R.mipmap.icon_sex_unknow
                    }
                }
            }

            else -> {
                tvItemRingId.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                ringIdTvColor = R.color.comm_text_gray_dark
                sexIcon = R.mipmap.icon_female
                ringIdBg = R.drawable.round8_female_pigeon_linear_bg
            }
        }
        holder.setBackgroundResource(R.id.tvItemRingId, ringIdBg)
        holder.setImageResource(R.id.ivItemSex, sexIcon)
        holder.setTextColor(R.id.tvItemRingId, ContextCompat.getColor(context, ringIdTvColor))
    }

    // 更新单个 item 的方法
    fun updateItem(position: Int, dataBean: EditBloodInfoBean?) {
        if (position >= 0 && position < data.size) {
            val item: EditBloodInfoBean = data[position]
            if (dataBean != null) {
                item.country = dataBean.country
                item.countryEn = dataBean.countryEn
                item.featherColor = dataBean.featherColor
                item.ringId = dataBean.ringId
                item.name = dataBean.name
                item.sex = dataBean.sex
                item.eye = dataBean.eye
                item.gradesDesc = dataBean.gradesDesc
                item.title = dataBean.title
                item.level = dataBean.level
                item.levelInfo = dataBean.levelInfo
            }
            notifyItemChanged(position)
        }
    }
}